package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private   CheckGroupDao groupDao;

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = groupDao.findAll();
        return checkGroups;
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        if (queryPageBean != null){
            //当查询条件不为“”时，设置页码为1
//            if (queryPageBean.getQueryString() != null) {
//                String strip = queryPageBean.getQueryString().strip();
//                queryPageBean.setQueryString(strip);
//
//                queryPageBean.setCurrentPage(1);
//
//            }

            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

            List<CheckGroup> groups = groupDao.findPage(queryPageBean);
            Page<CheckGroup> page = (Page<CheckGroup>) groups;
            if (page != null){
                return new PageResult(page.getTotal(),page.getResult());
            }else {
                return null;
            }
        }

        return null;
    }

    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        //插入group,获取group 主键
        groupDao.addGroupAndGetId(checkGroup);
        //插入group_item关系表
        if (checkGroup.getId() != null && checkGroup.getId() >0)
        groupDao.addGroupIdAndItemId(checkGroup.getId(),checkItemIds);
    }

    //根据id查group
    @Transactional
    @Override
    public CheckGroup findById(Integer id) {
        if (id != null && id > 0){
            CheckGroup group = groupDao.findById(id);
            //查询对应itemList
            List<CheckItem> items = groupDao.findItemsByGroupId(id);
            group.setCheckItems(items);
            return group;
        }

        return null;
    }

    //修改组
    @Transactional
    @Override
    public Integer editGroup(CheckGroup checkGroup, Integer[] checkItems) {
        if (checkGroup != null && checkGroup.getId() != null){
            //修改组
            int row = groupDao.updateGroup(checkGroup);
            if (row != 1){
                //删除group失败，结束
                return null;
            }
            //修改绑定项
                //删除原来的项
            groupDao.deletItemByGroupId(checkGroup.getId());
                //重新插入group_item
            groupDao.addGroupIdAndItemId(checkGroup.getId(),checkItems);
            //结束，返回row
            return row;
        }


        return null;
    }


}
